package day20240922;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.*;
import java.util.Date;

@WebServlet("/day20240922/Add")
public class Add extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String age1 = req.getParameter("age");
        Integer age = Integer.valueOf(age1);
        String sex = req.getParameter("sex");
        String date1 = req.getParameter("birthday");
        String[] split = date1.split("-");
        Integer year = Integer.valueOf(split[0]);
        Integer yue = Integer.valueOf(split[1]);
        Integer ri = Integer.valueOf(split[2]);
        Date date0 = new Date(year-1900, yue, ri);
        java.sql.Date date = new java.sql.Date(date0.getTime());

        try {
            Connection connection = getConnection();
            String sql = "INSERT INTO mm (id,name,age,sex,birthday) VALUES (?,?,?,?,?);";
            String getID = "SELECT MAX(id) AS max_id FROM mm;";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(getID);
            int maxid = 0;
            if (resultSet.next()) {
                maxid = resultSet.getInt("max_id");
            }
            if (maxid == 0){
                resp.getWriter().write("<html><body>主键输入错误</body></html>");
                return ;
            }
            preparedStatement.setInt(1, maxid + 1);
            preparedStatement.setString(2, name);
            preparedStatement.setInt(3, age);
            preparedStatement.setString(4, sex);
            long time = date.getTime();
            java.sql.Date d = new java.sql.Date(time);
            preparedStatement.setDate(5, d);
            int rowsInserted = preparedStatement.executeUpdate();
            if (rowsInserted > 0) {
                System.out.println("数据添加成功！");
                resp.getWriter().write("<!DOCTYPE html>\n" +
                        "<html lang=\"en\">\n" +
                        "<head>\n" +
                        "    <meta charset=\"UTF-8\">\n" +
                        "    <title>管理页面</title>\n" +
                        "</head>\n" +
                        "<body>\n" +
                        "<a href=\"manage_jsp.jsp\" style = \"font-size = \"100px\"\">添加完成点击返回管理界面</a>" +
                        "</body>\n" +
                        "</html>");
            }
            preparedStatement.close();
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    public Connection getConnection() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        System.out.println("JDBC驱动加载成功！");
        String url = "jdbc:mysql://localhost:3306/manageman";
        String user = "root";
        String password = "lcw0909.";
        Connection connection = DriverManager.getConnection(url, user, password);
        return connection;
    }
}
